var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function base64encode(str) {
    var out, i, len;
    var c1, c2, c3;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
    c1 = str.charCodeAt(i++) & 0xff;
    if(i == len)
    {
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt((c1 & 0x3) << 4);
        out += "==";
        break;
    }
    c2 = str.charCodeAt(i++);
    if(i == len)
    {
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
        out += base64EncodeChars.charAt((c2 & 0xF) << 2);
        out += "=";
        break;
    }
    c3 = str.charCodeAt(i++);
    out += base64EncodeChars.charAt(c1 >> 2);
    out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
    out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
    out += base64EncodeChars.charAt(c3 & 0x3F);
    }
    return out;
}
function utf16to8(str) {
    var out, i, len, c;

    out = "";
    len = str.length;
    for(i = 0; i < len; i++) {
    c = str.charCodeAt(i);
    if ((c >= 0x0001) && (c <= 0x007F)) {
        out += str.charAt(i);
    } else if (c > 0x07FF) {
        out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
        out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
    } else {
        out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
    }
    }
    return out;
}
// JavaScript Document
// 刷新验证码
function changeCode() {
	//$("#LoginCode").attr('src',"{:U('Admin/verify')}/"+Math.random());
	var change=document.getElementById('LoginCode');
    change.src="/index.php/Admin/Login/verify?"+Math.random();
}

function adminLogin() {
	var username=$("input[name=username]").val();
	var password=$("input[name=password]").val();

    // var code = $("input[name=code]").val();
	var code = '';

		if(username ==''){
			alert('请输入用户名！');
			$("input[name=username]").focus();
			return false;
		}
		if(password =='' || password.length <5){
			alert('密码不能为空或小于5个字符！');
			$("input[name=password]").focus();
			return false;
		}
		/*if(code ==''){
			alert('请输入验证码！');
			$("input[name=code]").focus();
			return false;
		}*/
        username = base64encode(utf16to8(username));
        password = base64encode(utf16to8(password));
		$.ajax({
			type:"POST",
			//url:"{:U('Admin/checkLogin')}",
			url:"/index.php/Admin/Login/Login",
			dataType:"json",
			data:{"username":username,"password":password,"code":code},
			success:function(data){
				if (data.status == "success") {
					//alert(data.message);
                    //alert(data.url);
					window.location.href=data.url;
					//window.location.href="/index.php/Admin/Index/index";
				} else {
					alert(data.message);
				    //刷新验证码
					if (data.code == 1) {
						$("input[name=code]").val("");
						changeCode();
					}
				}
				return false;
			},
			error: function () { alert('error！',"error！") }
		});
		return false;

}